/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import domain.model.IRegistro;
import domain.model.rms.UsuarioConfig;
import javax.microedition.io.ConnectionNotFoundException;
import util.LwuitAppHelper;
import view.img.ImageStore;

/**
 *
 * @author ademar.victorino
 */
public class DiscarForm extends AbstractForm implements ActionListener {

    private IRegistro reg;
    private AbstractForm returnForm;
    private Button btnTel1 = new Button();
    private Button btnTel2 = new Button();

    public DiscarForm() {
        super();

        setDefaultBtnStyle(btnTel1);
        setDefaultBtnStyle(btnTel2);

        setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addComponent(btnTel1);
        addComponent(btnTel2);

        btnTel1.setIcon(ImageStore.getInstance().getImage("Disk"));
        btnTel2.setIcon(ImageStore.getInstance().getImage("Disk"));

        btnTel1.setFocus(true);

        btnTel1.addActionListener(this);
        btnTel2.addActionListener(this);

        addCommand(new Command("Voltar") {

            public void actionPerformed(ActionEvent evt) {
                returnForm.show();
            }
        });

        addCommand(new Command("Operadora") {

            public void actionPerformed(ActionEvent evt) {

                new ConfigOperadoraForm(DiscarForm.this).show();
            }
        });
    }

    void show(IRegistro reg, AbstractForm returnForm) {
        this.returnForm = returnForm;
        this.reg = reg;

        btnTel1.setText("  " + reg.getTelefone());
        
        if (!"".equals(reg.getTelefone2())) {
            btnTel2.setText("  " + reg.getTelefone2());
            setTitle("Selecione o telefone");
        } else {
            removeComponent(btnTel2);
            setTitle("Discar");
        }
        super.show();
    }

    public void actionPerformed(ActionEvent arg0) {

        try {
            String tel = "";
            UsuarioConfig uc = LwuitAppHelper.getInstance().getUc();
            if (uc.getUsarInterurbano().booleanValue()) {
                tel += uc.getOperadora();
                tel += reg.getDDD();
            }
            tel += ((Button)arg0.getSource()).getText().trim();
            LwuitAppHelper.getInstance().getMidletDefault().platformRequest("tel:" + tel);

        } catch (ConnectionNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}
